package org.example.springioc01;

import lombok.extern.slf4j.Slf4j;
import org.example.springioc01.bean.*;
import org.example.springioc01.dao.DeliveryDAO;
import org.example.springioc01.dao.MyDataSource;
import org.example.springioc01.dao.UserDao;
import org.example.springioc01.service.HahaService;
import org.example.springioc01.service.UserService;
import org.springframework.asm.ClassWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;


@SpringBootApplication


//导入这个类的组件对象到IOC中
//@Import(DynamicCallSiteDesc.class)
//组件扫描路径,扫描相关注解的类
//@ComponentScan(basePackages = "org.example")

//@Import(Car.class)
@Slf4j
public class SpringIoc01Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        System.out.println("ioc容器创建完成");
        Animal animal = ioc.getBean(Animal.class);
        System.out.println(animal);
    }

    public static void maintest07(String[] args) {   //profile注解
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        System.out.println("ioc容器创建完成");
        DeliveryDAO deliveryDAO = ioc.getBean(DeliveryDAO.class);
        System.out.println(deliveryDAO);
        deliveryDAO.save();
    }
    public static void maintest06(String[] args) { //Value注解
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        System.out.println("ioc容器创建完成");
        Cat cat = ioc.getBean(Cat.class);
        System.out.println(cat);

    }


    public static void maintext05(String[] args) { //感知接口
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        System.out.println("ioc容器创建完成");
        HahaService hahaService = ioc.getBean(HahaService.class);
        System.out.println(hahaService);
        System.out.println(hahaService.getOsType());
        System.out.println(hahaService.getMyName());
    }
    public static void mainetst04(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        System.out.println("ioc容器创建完成");
        UserDao userDao = ioc.getBean(UserDao.class);
        System.out.println(userDao);
    }


    public static void maintest03(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        log.info("ioc加载完毕");
        Car car = ioc.getBean(Car.class);
        System.out.println(car);


        Person person = ioc.getBean(Person.class);
        System.out.println(person);

        //获取环境变量
        ConfigurableEnvironment environment =  ioc.getEnvironment();
        //获取系统
       String property = environment.getProperty("OS");
        System.out.println(property);
        System.out.println("-------------------------");
       /* Person p = (Person)ioc.getBean("person");
        System.out.println(p);*/


        System.out.println("-----------------------2");
        Map<String,Dog> dogs = ioc.getBeansOfType(Dog.class);
        System.out.println(dogs);
    }

    /**
     * 默认,分层注解能起作用的前提是,这些组件必须在主程序所在的包及其子包.
     * spring为我们提供了快速的MVC分成注解
     * 1.@Controller 控制器
     * 2.@Service 服务层
     * 3.@Repository 持久层
     * 4.@Component 组件(通用注解)
     * @param args
     */
    public static void maintest02(String[] args) {

        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        UserService us = ioc.getBean(UserService.class);
        System.out.println(us);
        System.out.println(us.getN());


    }

    public static void maintest01(String[] args) {
        //项目一启动ioc容器里面就有默认组件
        ConfigurableApplicationContext ioc = SpringApplication.run(SpringIoc01Application.class, args);
        //获取所有的组件名
        String[] s = ioc.getBeanDefinitionNames();
        System.out.println();
        //默认组件遍历出来
        for (String string : s) {
            System.out.println(string);
        }
        //获取容器中的组件对象
        //组件的四大特性:名字,类型,对象,作用域


        //通过名字获取组件
        //NoSuchBeanDefinitionException错误是没有相应组件
        Object obj = ioc.getBean("hahaha");
        System.out.println("对象 = "+obj);

        //通过组件获取类型
        //没有根据类型获取组件的时候,这个类型有多个对象
        // 就会报错NoUniqueBeanDefinitionException(没有唯一bean)
        //Dog dog = ioc.getBean(Dog.class);
        //System.out.println(dog);



        Dog dog = (Dog)ioc.getBean("dog");
        System.out.println(dog);


    }









}
